package com.architecture.util.object;

/**
 * <p>This class has utilities methods for be used with Objects.</p>
 * @author JR
 * @version 1.0
 * @category Object Utilities
 */
public final class ObjectUtility {
	
	/**
	 * private constructor of <code>ObjectUtility</code>.
	 */
	private ObjectUtility() {
		super();
	}
	
	/**
	 * <p>Verify if a reference is not null.</p>
	 * @param reference
	 * @return <code>Boolean</code>
	 */
	public static Boolean isRef(final Object reference){
		return ( reference != null );
	}

	/**
	 * <p>Verify if a reference is null.</p>
	 * @param reference
	 * @return <code>Boolean</code>
	 */
	public static Boolean isNotRef(final Object reference){
		return !isRef(reference);
	}
	
	/**
	 * <p>Verify if a arrays of reference is null.</p>
	 * @param objects
	 * @return <code>Boolean</code>
	 */
	public static <T extends Object> Boolean isAllRef(final T...objects){
		if(isRef(objects)){
			Boolean found = Boolean.TRUE;
			for(final Object object:objects){
				if(found){
					found = isRef(object);
				}else{
					break;
				}
			}
			return found;
		}else{
			return Boolean.FALSE;
		}
	}
	
	/**
	 * <p>Creates a new instance of an object.</p>
	 * @param object
	 * @return <code>E</code>
	 */
	public static <E> E create(Class<E> object){
		try {
			if(ObjectUtility.isRef(object)){
				return null;
			}else{
				return (E) object.newInstance();	
			}
		} catch (Exception e) {
			e.printStackTrace(System.err);
			return null;
		} 
	}
	
}
